~ chicken-core (chicken-5) /manual/Module (chicken tcp)


  1[[tags: manual]]
  2[[toc:]]
  3
  4== Module (chicken tcp)
  5
  6This module provides basic facilities for communicating over TCP
  7sockets.  
  8
  9All errors related to failing network operations will raise a condition
 10of kind {{(exn i/o net)}}.
 11
 12
 13=== tcp-listen
 14
 15<procedure>(tcp-listen TCPPORT [BACKLOG [HOST]])</procedure>
 16
 17Creates and returns a TCP listener object that listens for connections on {{TCPPORT}}, which
 18should be an exact integer. {{BACKLOG}} specifies the number of maximally pending
 19connections (and defaults to 100). If the optional argument {{HOST}} is given and not
 20{{#f}}, then only incoming connections for the given host (or IP) are accepted.
 21
 22
 23=== tcp-listener?
 24
 25<procedure>(tcp-listener? X)</procedure>
 26
 27Returns {{#t}} if {{X}} is a TCP listener object, or {{#f}} otherwise.
 28
 29
 30=== tcp-close
 31
 32<procedure>(tcp-close LISTENER)</procedure>
 33
 34Reclaims any resources associated with {{LISTENER}}.
 35
 36
 37=== tcp-accept
 38
 39<procedure>(tcp-accept LISTENER)</procedure>
 40
 41Waits until a connection is established on the port on which
 42{{LISTENER}} is listening and returns two values: an input- and
 43output-port that can be used to communicate with the remote
 44process. The current value of {{tcp-accept-timeout}} is used to
 45determine the maximal number of milliseconds (if any) to wait
 46until a connection is established. When a client connects any 
 47read- and write-operations on the returned ports will use the
 48current values (at the time of the connection) of {{tcp-read-timeout}}
 49and {{tcp-write-timeout}}, respectively, to determine the maximal
 50number of milliseconds to wait for input/output before a timeout
 51error is signalled.
 52
 53Note: this operation and any I/O on the ports returned will not block
 54other running threads.
 55
 56
 57=== tcp-accept-ready?
 58
 59<procedure>(tcp-accept-ready? LISTENER)</procedure>
 60
 61Returns {{#t}} if there are any connections pending on {{LISTENER}}, or {{#f}}
 62otherwise.
 63
 64
 65=== tcp-listener-port
 66
 67<procedure>(tcp-listener-port LISTENER)</procedure>
 68
 69Returns the port number assigned to {{LISTENER}} (If you pass {{0}} to {{tcp-listen}},
 70then the system will choose a port-number for you).
 71
 72=== tcp-listener-fileno
 73
 74<procedure>(tcp-listener-fileno LISTENER)</procedure>
 75
 76Returns the file-descriptor associated with {{LISTENER}}.
 77
 78
 79=== tcp-connect
 80
 81<procedure>(tcp-connect HOSTNAME [TCPPORT])</procedure>
 82
 83Establishes a client-side TCP connection to the machine with the name
 84{{HOSTNAME}} (a string) at {{TCPPORT}} (an exact integer) and returns
 85two values: an input- and output-port for communicating with the
 86remote process. The current value of {{tcp-connect-timeout}} is used
 87to determine the maximal number of milliseconds (if any) to wait until
 88the connection is established. When the connection takes place any
 89read- and write-operations on the returned ports will use the current
 90values (at the time of the call to {{tcp-connect}}) of {{tcp-read-timeout}} and
 91{{tcp-write-timeout}}, respectively, to determine the maximal number
 92of milliseconds to wait for input/output before a timeout error is
 93signalled.
 94
 95If the {{TCPPORT}} is omitted, the port is parsed from the {{HOSTNAME}} string.  The format expected is {{HOSTNAME:PORT}}.  The {{PORT}} can either be a string representation of an integer or a service name which is translated to an integer using the POSIX function [[http://www.opengroup.org/onlinepubs/009695399/functions/getservbyname.html|{{getservbyname}}]].
 96
 97Note: any I/O on the ports returned will not block other running threads.
 98
 99
100=== tcp-addresses
101
102<procedure>(tcp-addresses PORT)</procedure>
103
104Returns two values for the input- or output-port {{PORT}} (which should be a port returned
105by either {{tcp-accept}} or {{tcp-connect}}): the IP address of the local and the remote
106machine that are connected over the socket associated with {{PORT}}. The returned addresses
107are strings in {{XXX.XXX.XXX.XXX}} notation.
108
109
110=== tcp-port-numbers
111
112<procedure>(tcp-port-numbers PORT)</procedure>
113
114Returns two values for the input- or output-port {{PORT}} (which should be a port returned
115by either {{tcp-accept}} or {{tcp-connect}}): the TCP port numbers of the local and the remote
116machine that are connected over the socket associated with {{PORT}}.
117
118
119=== tcp-abandon-port
120
121<procedure>(tcp-abandon-port PORT)</procedure>
122
123Marks the socket port {{PORT}} as abandoned. This is mainly useful to close down a port
124without breaking the connection.
125
126
127=== tcp-buffer-size
128
129<parameter>tcp-buffer-size</parameter>
130
131Sets the size of the output buffer. By default no output-buffering for
132TCP output is done, but to improve performance by minimizing the
133number of TCP packets, buffering may be turned on by setting this
134parameter to an exact integer greater zero. A buffer size of zero or {{#f}}
135turns buffering off. The setting of this parameter takes effect at the time
136when the I/O ports for a particular socket are created, i.e. when {{tcp-connect}}
137or {{tcp-accept}} is called.
138
139Note that since output is not immediately written to the associated socket, you
140may need to call {{flush-output}}, once you want the output to be transmitted.
141Closing the output port will flush automatically.
142
143=== tcp-read-timeout
144
145<parameter>tcp-read-timeout</parameter>
146
147Determines the timeout for TCP read operations in milliseconds. A timeout of
148{{#f}} disables timeout checking. The default read timeout is 60000, i.e.
1491 minute.
150If timeout occurs while reading, a condition object of kinds {{(exn i/o net timeout)}}
151is thrown.
152
153=== tcp-write-timeout
154
155<parameter>tcp-write-timeout</parameter>
156
157Determines the timeout for TCP write operations in milliseconds. A timeout of
158{{#f}} disables timeout checking. The default write timeout is 60000, i.e.
1591 minute.
160If timeout occurs while writing, a condition object of kinds {{(exn i/o net timeout)}}
161is thrown.
162
163=== tcp-connect-timeout
164
165<parameter>tcp-connect-timeout</parameter>
166
167Determines the timeout for {{tcp-connect}} operations in milliseconds. A timeout of
168{{#f}} disables timeout checking and is the default.
169If timeout occurs while trying to connect, a condition object of kinds {{(exn i/o net timeout)}}
170is thrown.
171
172
173=== tcp-accept-timeout
174
175<parameter>tcp-accept-timeout</parameter>
176
177Determines the timeout for {{tcp-accept}} operations in milliseconds. A timeout of
178{{#f}} disables timeout checking and is the default.
179If timeout occurs while waiting for connections, a condition object of kinds {{(exn i/o net timeout)}}
180is thrown.
181
182
183=== Example
184
185A very simple example follows. Say we have the two files {{client.scm}}
186and {{server.scm}}:
187
188<enscript highlight=scheme>
189; client.scm
190(import (chicken io) (chicken tcp))
191(define-values (i o) (tcp-connect "localhost" 4242))
192(write-line "Good Bye!" o)
193(print (read-line i))
194</enscript>
195
196<enscript highlight=scheme>
197; server.scm
198(import (chicken io) (chicken tcp))
199(define l (tcp-listen 4242))
200(define-values (i o) (tcp-accept l))
201(write-line "Hello!" o)
202(print (read-line i))
203(close-input-port i)
204(close-output-port o)
205</enscript>
206
207 % csc server.scm
208 % csc client.scm
209 % ./server &
210 % ./client
211 Good Bye!
212 Hello!
213
214---
215Previous: [[Module (chicken syntax)]]
216
217Next: [[Module (chicken time)]]
Trap